package common.com.androidbase.fresco;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.android.sohu.sdk.common.toolbox.LogUtils;
import com.common.sdk.net.connect.http.ImageRequestManager;
import com.facebook.common.logging.FLog;
import com.facebook.common.util.UriUtil;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.DraweeView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.image.QualityInfo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import common.com.androidbase.R;
import common.com.pullrefresh.view.PullRefreshView;

public class MainActivity extends Activity {

    private static final String TAG = "MainActivity2";

    private static final String URL = "http://img.ivsky.com/img/tupian/pre/201606/27/yarlung_zangbo_grand_canyon-004.jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Button evictButton = (Button) findViewById(R.id.evict);
        evictButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageRequestManager.getInstance().evictMemoryCache(Uri.parse(URL));
                ImageRequestManager.getInstance().clearMemoryCache();
            }
        });

        Button button = (Button)findViewById(R.id.test);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 渐进图片
//                DraweeView progressiveImg = (DraweeView) findViewById(R.id.iv_progressive);
//                String url = "http://img.ivsky.com/img/tupian/pre/201606/27/yarlung_zangbo_grand_canyon-004.jpg";
//                Uri uri = Uri.parse("file:///sdcard/1726312jz4va2rw6r6agn2.jpg");
//                ImageRequestManager.getInstance().startImageRequest(progressiveImg, uri, 500, 250);
//                ImageRequestManager.getInstance().startProgressiveRequest(progressiveImg, url, 500, 250);

                // gif图片
//        SimpleDraweeView gifView = (SimpleDraweeView) findViewById(R.id.iv_gif);
//        ImageRequestManager.getInstance().startGifRequest(gifView, "https://upload.wikimedia.org/wikipedia/en/1/18/Morphing_3D_graph.gif");

                File file = ImageRequestManager.getInstance().getImageCachedFile("https://upload.wikimedia.org/wikipedia/en/1/18/Morphing_3D_graph.gif");
                LogUtils.e(TAG , "file.path : " + file.getPath());


                //加载本地图片
//        Uri uri = Uri.parse("file:///sdcard/test.jpg");
//        ImageRequestManager.getInstance().startImageRequest(draweeView, uri);

//                final String url = "http://s1.tv.itc.cn/picture/distribute_platform/1/491/1730/icon/cc600196e5efc9949818d905e198dfd3_128_128.png";

//                Log.d(TAG, "bitmap is in cache : " + ImageRequestManager.getInstance().isInBitmapMemoryCache(URL));
//
//                ImageRequestManager.getInstance().startImageRequest(progressiveImg, URL);


        // 测试只从缓存取
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    Thread.sleep(3000);
//                    Log.d(TAG, "bitmap is in cache : " + ImageRequestManager.getInstance().isInBitmapMemoryCache(URL));
//                    Bitmap bitmap = ImageRequestManager.getInstance().startImageRequestCacheOnly(URL);
//                    if (bitmap != null) {
//                        Log.d(TAG, "bitmap" + bitmap.toString());
//                    } else {
//                        Log.d(TAG, "bitmap is null");
//                    }
//                } catch (Exception e) {
//                    LogUtils.e(TAG, e);
//                }
//            }
//        }).start();
//
//
//        // 测试不产生缓存
//        final DraweeView normalView = (DraweeView) findViewById(R.id.iv_normal);
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                for(int i=0; i<10; i++) {
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            ImageRequestManager.getInstance().startImageRequestNoCache(normalView, "http://img.ivsky.com/img/tupian/pre/201606/27/yarlung_zangbo_grand_canyon-004.jpg");
//                        }
//                    });
//
//                    try {
//                        Thread.sleep(1000);
//                    } catch (Exception e) {
//                        LogUtils.e(TAG, e);
//                    }
//                }
//            }
//        }).start();
//
                // listview中使用
                PullRefreshView listView = (PullRefreshView) findViewById(R.id.pullrefresh);
                List<TwoImages> images = new ArrayList<TwoImages>();
                TwoImages images1 = new TwoImages();
                TwoImages images2 = new TwoImages();
                TwoImages images3 = new TwoImages();
                TwoImages images4 = new TwoImages();
                TwoImages images5 = new TwoImages();
                TwoImages images6 = new TwoImages();
                TwoImages images7 = new TwoImages();
                TwoImages images8 = new TwoImages();
                TwoImages images9 = new TwoImages();
                TwoImages images10 = new TwoImages();
                TwoImages images11 = new TwoImages();
                TwoImages images12 = new TwoImages();
                TwoImages images13 = new TwoImages();
                TwoImages images14 = new TwoImages();
                TwoImages images15 = new TwoImages();
                TwoImages images16 = new TwoImages();
                TwoImages images17 = new TwoImages();
                TwoImages images18 = new TwoImages();
                TwoImages images19 = new TwoImages();



                images1.setLeftUrl("http://img.ivsky.com/img/tupian/t/201308/30/yueyuanzhiye-003.jpg");
                images1.setRightUrl("http://img.ivsky.com/img/tupian/t/201308/30/yueyuanzhiye-014.jpg");

                images2.setLeftUrl("http://img.ivsky.com/img/tupian/t/201308/30/yueyuanzhiye-015.jpg");
                images2.setRightUrl("http://img.ivsky.com/img/tupian/t/201308/30/yueyuanzhiye-016.jpg");

                images3.setLeftUrl("http://img.ivsky.com/img/tupian/t/201308/30/yueyuanzhiye-017.jpg");
                images3.setRightUrl("http://img.ivsky.com/img/tupian/t/201308/30/yueyuanzhiye-018.jpg");

                images4.setLeftUrl("http://img.ivsky.com/img/tupian/t/201308/30/yueyuanzhiye-019.jpg");
                images4.setRightUrl("http://img.ivsky.com/img/tupian/t/201308/30/yueyuanzhiye-020.jpg");

                images5.setLeftUrl("http://img.ivsky.com/img/tupian/t/201308/30/yueyuanzhiye-021.jpg");
                images5.setRightUrl("http://img.ivsky.com/img/tupian/t/201308/30/yueyuanzhiye-004.jpg");

                images6.setLeftUrl("http://img.ivsky.com/img/tupian/t/201308/30/yueyuanzhiye-005.jpg");
                images6.setRightUrl("http://img.ivsky.com/img/tupian/t/201308/30/yueyuanzhiye-006.jpg");

                images7.setLeftUrl("http://img.ivsky.com/img/tupian/t/201308/30/yueyuanzhiye-007.jpg");
                images7.setRightUrl("http://img.ivsky.com/img/tupian/t/201308/30/yueyuanzhiye-008.jpg");

                images8.setLeftUrl("http://img.ivsky.com/img/tupian/t/201308/30/yueyuanzhiye-009.jpg");
                images8.setRightUrl("http://img.ivsky.com/img/tupian/t/201308/30/yueyuanzhiye-010.jpg");

                images9.setLeftUrl("http://img.ivsky.com/img/tupian/t/201308/30/yueyuanzhiye-011.jpg");
                images9.setRightUrl("http://img.ivsky.com/img/tupian/t/201308/30/yueyuanzhiye-022.jpg");

                images10.setLeftUrl("http://img.ivsky.com/img/tupian/t/201606/27/beidaihe-001.jpg");
                images10.setRightUrl("http://img.ivsky.com/img/tupian/t/201606/27/beidaihe-002.jpg");

                images11.setLeftUrl("http://img.ivsky.com/img/tupian/t/201606/27/beidaihe-003.jpg");
                images11.setRightUrl("http://img.ivsky.com/img/tupian/t/201606/27/beidaihe-004.jpg");

                images12.setLeftUrl("http://img.ivsky.com/img/tupian/t/201606/27/beidaihe-005.jpg");
                images12.setRightUrl("http://img.ivsky.com/img/tupian/t/201606/27/beidaihe-006.jpg");

                images13.setLeftUrl("http://img.ivsky.com/img/tupian/t/201606/27/beidaihe-007.jpg");
                images13.setRightUrl("http://img.ivsky.com/img/tupian/t/201606/27/beidaihe-008.jpg");

                images14.setLeftUrl("http://img.ivsky.com/img/tupian/t/201606/27/beidaihe-009.jpg");
                images14.setRightUrl("http://img.ivsky.com/img/tupian/t/201606/27/beidaihe-010.jpg");

                images15.setLeftUrl("http://img.ivsky.com/img/tupian/t/201606/26/rose-001.jpg");
                images15.setRightUrl("http://img.ivsky.com/img/tupian/t/201606/26/rose-002.jpg");

                images16.setLeftUrl("http://img.ivsky.com/img/tupian/t/201606/26/rose-003.jpg");
                images16.setRightUrl("http://img.ivsky.com/img/tupian/t/201606/26/rose-004.jpg");

                images17.setLeftUrl("http://img.ivsky.com/img/tupian/t/201606/26/rose-005.jpg");
                images17.setRightUrl("http://img.ivsky.com/img/tupian/t/201606/26/rose-006.jpg");

                images18.setLeftUrl("http://img.ivsky.com/img/tupian/t/201606/26/rose-007.jpg");
                images18.setRightUrl("http://img.ivsky.com/img/tupian/t/201606/26/rose-008.jpg");

                images19.setLeftUrl("http://img.ivsky.com/img/tupian/t/201606/26/rose-009.jpg");
                images19.setRightUrl("http://img.ivsky.com/img/tupian/t/201606/26/rose-010.jpg");

                images.add(images1);
                images.add(images2);
                images.add(images3);
                images.add(images4);
                images.add(images5);
                images.add(images6);
                images.add(images7);
                images.add(images8);
                images.add(images9);
                images.add(images10);
                images.add(images11);
                images.add(images12);
                images.add(images13);
                images.add(images14);
                images.add(images15);
                images.add(images16);
                images.add(images17);
                images.add(images18);
                images.add(images19);


                TwoColumnImageAdater adapter = new TwoColumnImageAdater(MainActivity.this, listView, images);
                listView.setAdapter(adapter);
            }
        });


    }
}
